/**
 * 游戏交互服务
 * 负责处理游戏中的玩家交互和事件
 */

export default {
  /**
   * 处理财产交换
   * @param {Object} gameState 游戏状态
   * @param {number} currentPlayerIndex 当前玩家索引
   * @param {number} targetPlayerId 目标玩家ID
   * @param {Function} showEventMessage 显示事件消息的回调
   * @returns {Object} 交换状态
   */
  handlePropertyExchange(gameState, currentPlayerIndex, targetPlayerId, showEventMessage) {
    const currentPlayer = gameState.players[currentPlayerIndex];
    const targetPlayer = gameState.players.find(p => p.id === targetPlayerId);
    
    if (!targetPlayer) {
      return { success: false, reason: 'player_not_found' };
    }
    
    // 获取当前玩家的地产
    const currentPlayerProperties = gameState.boardCells.filter(cell => cell.owner === currentPlayer.id);
    
    // 显示当前玩家选择要交换的地产
    const propertyActions = currentPlayerProperties.map(cell => ({
      text: `${cell.name} (租金: ${cell.rent}元)`,
      action: 'selectOwnProperty',
      cellIndex: gameState.boardCells.indexOf(cell)
    }));
    
    propertyActions.push({ text: '取消', action: 'confirm' });
    
    showEventMessage(
      '选择要交换的地产', 
      `请选择你要交换的地产：`, 
      propertyActions
    );
    
    return { success: true, targetPlayerId };
  },
  
  /**
   * 选择自己的地产后，选择对方的地产
   * @param {Object} gameState 游戏状态
   * @param {number} cellIndex 选择的单元格索引
   * @param {number} targetPlayerId 目标玩家ID
   * @param {Function} showEventMessage 显示事件消息的回调
   * @returns {Object} 选择状态
   */
  handleSelectOwnProperty(gameState, cellIndex, targetPlayerId, showEventMessage) {
    const targetPlayer = gameState.players.find(p => p.id === targetPlayerId);
    
    if (!targetPlayer) {
      return { success: false, reason: 'player_not_found' };
    }
    
    // 获取目标玩家的地产
    const targetPlayerProperties = gameState.boardCells.filter(cell => cell.owner === targetPlayer.id);
    
    // 显示目标玩家的地产列表
    const propertyActions = targetPlayerProperties.map(cell => ({
      text: `${cell.name} (租金: ${cell.rent}元)`,
      action: 'completeExchange',
      ownCellIndex: cellIndex,
      targetCellIndex: gameState.boardCells.indexOf(cell)
    }));
    
    propertyActions.push({ text: '取消', action: 'confirm' });
    
    showEventMessage(
      '选择对方地产', 
      `请选择${targetPlayer.name}的一处地产进行交换：`, 
      propertyActions
    );
    
    return { success: true };
  },
  
  /**
   * 完成交换
   * @param {Object} gameState 游戏状态
   * @param {number} currentPlayerIndex 当前玩家索引
   * @param {number} ownCellIndex 自己的单元格索引
   * @param {number} targetCellIndex 目标单元格索引
   * @param {number} targetPlayerId 目标玩家ID
   * @param {Function} showEventMessage 显示事件消息的回调
   * @returns {Object} 交换结果
   */
  completePropertyExchange(gameState, currentPlayerIndex, ownCellIndex, targetCellIndex, targetPlayerId, showEventMessage) {
    const currentPlayer = gameState.players[currentPlayerIndex];
    const targetPlayer = gameState.players.find(p => p.id === targetPlayerId);
    
    if (!targetPlayer) {
      return { success: false, reason: 'player_not_found' };
    }
    
    // 交换地产所有权
    const ownCell = gameState.boardCells[ownCellIndex];
    const targetCell = gameState.boardCells[targetCellIndex];
    
    ownCell.owner = targetPlayer.id;
    targetCell.owner = currentPlayer.id;
    
    // 更新玩家的地产列表
    currentPlayer.properties = gameState.boardCells
      .filter(cell => cell.owner === currentPlayer.id)
      .map(cell => cell.name);
      
    targetPlayer.properties = gameState.boardCells
      .filter(cell => cell.owner === targetPlayer.id)
      .map(cell => cell.name);
    
    // 显示交换成功消息
    showEventMessage(
      '交换成功', 
      `你用${ownCell.name}成功交换了${targetPlayer.name}的${targetCell.name}！`, 
      [{ text: '太好了', action: 'confirm' }]
    );
    
    return { success: true };
  },
  
  /**
   * 初始化财产交换
   * @param {Object} gameState 游戏状态
   * @param {Function} showEventMessage 显示事件消息的回调
   * @returns {boolean} 是否成功初始化交换
   */
  initiatePropertyExchange(gameState, showEventMessage) {
    const currentPlayer = gameState.players[gameState.currentPlayerIndex];
    
    // 检查当前玩家是否有地产
    const playerProperties = gameState.boardCells.filter(cell => cell.owner === currentPlayer.id);
    
    if (playerProperties.length === 0) {
      showEventMessage(
        '没有地产', 
        '你没有任何地产可以交换。', 
        [{ text: '确定', action: 'confirm' }]
      );
      return false;
    }
    
    // 检查其他玩家是否有地产
    const otherPlayersWithProperties = gameState.players.filter(p => {
      if (p.id === currentPlayer.id) return false;
      return gameState.boardCells.some(cell => cell.owner === p.id);
    });
    
    if (otherPlayersWithProperties.length === 0) {
      showEventMessage(
        '无法交换', 
        '其他玩家没有任何地产可以交换。', 
        [{ text: '确定', action: 'confirm' }]
      );
      return false;
    }
    
    // 显示选择玩家的对话框
    const playerActions = otherPlayersWithProperties.map(p => ({
      text: `与${p.name}交换`,
      action: 'selectExchangePlayer',
      playerId: p.id
    }));
    
    playerActions.push({ text: '取消', action: 'confirm' });
    
    showEventMessage(
      '选择交换对象', 
      '请选择要与哪位玩家交换地产：', 
      playerActions
    );
    
    return true;
  }
};